﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using System.Collections;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.ADF;
using PlanningGIS.Framework;
using PlanningGIS.Util.Log;

namespace PlanningGIS.DataConvert.model
{
  
    public class DlgDataExport : Operator2
    {
        private int totalCount;
        private int rightCount;
        private int errorCount;

        private List<IFeatureClass> pFeatClsLstTarget;//导出要素类列表，方便加载导出结果
        private List<DlgDataConvert> dlgDataConverts = null;

        private bool needRecord;

        /// </summary>
        /// 导出要素类列表，方便加载导出结果
        /// </summary>
        public List<IFeatureClass> FeatClsListTarget
        {
            get { return pFeatClsLstTarget; }
        }

        /// <summary>
        /// 数据转换任务列表
        /// </summary>
        public List<DlgDataConvert> Task
        {
            set { dlgDataConverts = value; }
        }

        /// <summary>
        /// 是否需要记录导出的图层，以便操作完成后进行加载
        /// </summary>
        public bool NeedRecord
        {
            set { needRecord = value; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public DlgDataExport()
        {
            totalCount = 0;
            rightCount = 0;
            errorCount = 0;

            pFeatClsLstTarget = new List<IFeatureClass>();
        }

        /// <summary>
        /// 重写停止操作事件，使得本次任务中的每一项皆停止入库
        /// </summary>
        /// <returns></returns>
        public override bool Stop()
        {

            if (dlgDataConverts != null && dlgDataConverts.Count != 0)
            {
                foreach (DlgDataConvert dlgDataConvert in dlgDataConverts)
                {
                    dlgDataConvert.Stop();
                }
            }

            return base.Stop();
        }

        /// <summary>
        /// DLG数据导出
        /// </summary>
        /// <returns></returns>
        public override bool Operate()
        {
            if (dlgDataConverts == null || dlgDataConverts.Count == 0)
            {
               LogHelper.WriteErrLog("导出任务未设置！");
                return false;
            }

            setProcessBarMax(dlgDataConverts.Count);
            int count = 0;
            foreach (DlgDataConvert dlgDataConvert in dlgDataConverts)
            {
                setProcessBarCurrent(++count);

                dlgDataConvert.Operate();

                totalCount = totalCount + dlgDataConvert.TotalCount;
                errorCount = errorCount + dlgDataConvert.ErrorCount;
                rightCount = rightCount + dlgDataConvert.RightCount;

                if (needRecord)
                {
                    if (!pFeatClsLstTarget.Contains(dlgDataConvert.TargetFeatCls))
                        pFeatClsLstTarget.Add(dlgDataConvert.TargetFeatCls);
                }
                if (stopAnyway) return false;
            }
            if (errorCount > 0)
               LogHelper.WriteErrLog("数据导出完毕。待导出总数：" + totalCount + "个；成功：" + rightCount + "个；失败：" + errorCount + "个。\n");
            else
                LogHelper.WriteDoneLog("数据导出完毕。待导出总数：" + totalCount + "个；成功：" + rightCount + "个；失败：" + errorCount + "个。\n");

            return true;
        }
    }
}
